package icedot.library.common.ui;

import icedot.library.common.R;
import icedot.library.common.utils.CommonFun;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class UpdateFoot extends LinearLayout
{
	public enum Status
	{
		RESET,		  //复位
		PRESSEDDOWN,  //按下
		PRESSEDMOVE,  //按下移动
		PRESSEDUP,	  //松开
		UPDATEDATA,   //等待数据
		FINISHALLDATA,//数据已经全部加载完成
	}

	private String[] _hintList = new String[4]; //每个动作的提示
	private LinearLayout 		_layoutMain;
	private ImageView 			_imageLoading;
	private TextView 			_textContent;
	
	private int 				_minLoadSize;   	//拉动更新的最小尺寸
	private int 				_maxLoadSize;     	//最多能拉多高
	private Status 				_state;
	private UpdateViewListener  _notify;

	public UpdateFoot(Context context)
	{
		super(context);
		initView(context);
	}
	
	public UpdateFoot(Context context, AttributeSet attrs)
	{
		super(context, attrs);
		initView(context);
	}

	/**
	 * 默认头不显示任何内容
	 * @param context
	 */
	private void initView(Context context)
	{
		LinearLayout.LayoutParams mainParams = new LinearLayout.LayoutParams(
				LayoutParams.MATCH_PARENT, 0);
		_layoutMain = (LinearLayout) LayoutInflater.from(context).inflate(
				R.layout.update_foot, null);
		addView(_layoutMain, mainParams);
		
		_imageLoading = (ImageView)findViewById(R.id.exlistviewfoot_image_loading);
		_textContent = (TextView)findViewById(R.id.exlistviewfoot_text_time);
		_textContent.setVisibility(View.GONE);
		_imageLoading.setVisibility(View.GONE);
		
		_minLoadSize = CommonFun.dip2px(context,60);
		_maxLoadSize = CommonFun.dip2px(context,120);

		_hintList[0] = "继续上拉...";
		_hintList[1] = "放开加载...";
		_hintList[2] = "正在加载...";
		_hintList[3] = "数据已经全部加载完成";
	}
	
	public void setContent(String content)
	{
		if(_textContent != null)
		{
			_textContent.setText(content);
		}
	}

	public void setNotifyListener(UpdateViewListener notify)
	{
		_notify = notify;
	}

	public void setContent(Status status, String content)
	{
		if(status == Status.PRESSEDDOWN || status == Status.PRESSEDMOVE)
		{
			_hintList[0] = content;
		}
		else if(status == Status.PRESSEDUP)
		{
			_hintList[1] = content;
		}
		else if(status == Status.UPDATEDATA)
		{
			_hintList[2] = content;
		}
		else if(status == Status.FINISHALLDATA)
		{
			_hintList[3] = content;
		}
	}

	/**
	 * 设置动画.和显示内容
	 */
	public void useFootContent()
	{
		_textContent.setVisibility(View.VISIBLE);
		_imageLoading.setVisibility(View.VISIBLE);
	}

	/**
	 * 设置加载时的高度
	 * @param height
     */
	public void setLoadHeight(int height)
	{
		_minLoadSize = height;
	}
	
	public void setVisiableHeight(int height)
	{
		if(height < 0)
			height = 0;
		LinearLayout.LayoutParams mainParams = 
				(LinearLayout.LayoutParams)_layoutMain.getLayoutParams();
		
		mainParams.height = height;
		_layoutMain.setLayoutParams(mainParams);
	}
	
	public int getVisiableHeight()
	{
		return _layoutMain.getHeight();
	}

	public Status getState()
	{
		return _state;
	}

	public void setState(Status state)
	{
		if(state == Status.UPDATEDATA)
		{
			return;
		}

		if(state == Status.RESET && (_state == Status.UPDATEDATA || _state == Status.FINISHALLDATA))
		{
			_state = state;
		}
		else if(_state != Status.UPDATEDATA && state != Status.RESET)
		{
			_state = state;
			if(state == Status.PRESSEDUP)
			{
				if(!needLoad())
				{
					_state = Status.RESET;
				}
				else
				{
					setContent(_hintList[2]);
				}
			}
			else if(state == Status.PRESSEDDOWN)
			{
				setContent(_hintList[0]);
			}
			else if(state == Status.PRESSEDMOVE)
			{
				if(getVisiableHeight() >= getMinLoadHeight())
				{
					setContent(_hintList[1]);
				}
			}
			else if(state == Status.FINISHALLDATA)
			{
				setContent(_hintList[3]);
			}
		}
	}

	public boolean needLoad()
	{
		if(_notify == null)
		{
			return false;
		}
		if(_state == Status.UPDATEDATA)
		{
			return true;
		}

		LinearLayout.LayoutParams mainParams =
				(LinearLayout.LayoutParams)_layoutMain.getLayoutParams();
		if(mainParams.height >= _minLoadSize)
		{
			_state = Status.UPDATEDATA;
			_notify.onFootLoad();
			setContent(_hintList[2]);
			return true;
		}
		else
		{
			return false;
		}
	}
	
	public int getMinLoadHeight()
	{
		return _minLoadSize;
	}
	public int getMaxLoadHeight()
	{
		return _maxLoadSize;
	}
}
